package com.definity.toolkit.web.taglib;

import javax.servlet.jsp.JspException;

public class ReviewTag extends ComponentFormTag {

	private static final long serialVersionUID = 4300706967384314152L;

	private Integer amount;
	private Integer start;
	private Integer value;
	
	public ReviewTag() {
		setAmount(5);
		setStart(0);
		setValue(-1);
	}
	
	public void setAmount(Integer amount) {
		if (amount <= 0)
			throw new IllegalArgumentException("amount can be more than zero");
		
		this.amount = amount;
	}
	
	public void setStart(Integer start) {
		this.start = start;
	}
	
	public void setValue(Integer value) {
		this.value = value;
	}
	
	@Override
	protected String createLabel(String id, String label, Object value, FormTag formTag) {
		return "";
	}

	@Override
	protected String getComponentName() {
		return "review";
	}

	@Override
	protected String createStartTag(String name, String id, Object value) throws JspException {
		return null;
	}

	@Override
	protected String createEndTag(String name, String id, Object value) throws JspException {
		StringBuilder html = new StringBuilder();

		if (value == null) {
			value = this.value;
		}
		
		int actualValue = Integer.valueOf(value.toString());
		
		html.append("<input type=\"hidden\" name=\"");
		html.append(id);
		html.append("\" id=\"");
		html.append(id);
		html.append("\" value=\"");
		html.append(valueToString(value));
		html.append("\" />");
		
		html.append("<ul id=\"");
		html.append(id + "_creview");
		html.append("\" ");
		html.append(createStyleClass("creview"));
		html.append(">");

		String style = "";
		
		for (int i = start; i < amount; i++) {
			html.append("<li id=\"");
			html.append(i);
			html.append("\" class=\"");
			
			style = "";
			if (i <= actualValue) {
				style = "review-checked";
			}
			
			html.append(style);
			html.append("\">");
			html.append("</li>");
		}
		
		html.append("</ul>");
		
		return html.toString();
	}
	
}
